{include file="./layout/header.html" /}
<body>
<div class="x-nav">
  <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:left" onClick="javaScript:history.go(-1)" title="返回">
    <i class="layui-icon" style="line-height:30px">&#xe65c;</i></a>

  <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right" href="javascript:location.replace(location.href);" title="刷新">
    <i class="layui-icon" style="line-height:30px">ဂ</i></a>
</div>
<div class="x-body">
  <div class="layui-row">
    <!--<form class="layui-form layui-col-md12 x-so">-->
      <!--<input class="layui-input" placeholder="开始日" name="start" id="start">-->
      <!--<input class="layui-input" placeholder="截止日" name="end" id="end">-->
      <!--<input type="text" name="username"  placeholder="请输入用户名" autocomplete="off" class="layui-input">-->
      <!--<button class="layui-btn"  lay-submit="" lay-filter="sreach"><i class="layui-icon">&#xe615;</i></button>-->
    <!--</form>-->
  </div>
  <xblock>
    <input type="hidden" class="sid" id="sid" value="{$sid}">
    <!--<button class="layui-btn layui-btn-danger" onclick="delAll()"><i class="layui-icon"></i>批量删除</button>-->
    <a class="layui-btn" href='{:url("Confermenu/create")}?sid={$sid}'><i class="layui-icon"></i>添加</a>
    {present name="$total"}
    <span class="x-right" style="line-height:40px">共有数据：<span >{$total}</span> 条</span>
    <input type="hidden" id="total" value="{$total}">
    {/present}
  </xblock>

  <div id="demo"></div>


</div>

<script src="__CDN__/lib/layuitree/layui.js"></script>
<script type="text/javascript">
    function del(id,sid) {
        // console.log(row)
        alert(id)
        //发异步删除数据
        $.ajax({
            dataType : 'json',
            type : 'POST',
            url : 'delete',
            // // 告诉jQuery不要去处理发送的数据
            // processData : false,
            // // 告诉jQuery不要去设置Content-Type请求头
            contentType : false,
            async: false,
            data : {id:id,sid:sid},
            success : function(msg){
                layer.alert(msg.msg, {icon: 6},function () {
                    // $(obj).parents("tr").remove();
                    layer.msg('已删除!',{icon:1,time:1000});
                });
            },
            error : function(XMLHttpRequest, textStatus, errorThrown) {
                alert(XMLHttpRequest.status + "," + textStatus);
            }
        });
    }
    /*用户-删除*/
    function member_del(id,sid,lang){

        layer.confirm('确认要删除吗？',function(index){
            //发异步删除数据
            $.ajax({
                dataType : 'json',
                type : 'POST',
                url : 'delete',

                async: false,
                data : {id:id,sid:sid,lang:lang},
                success : function(msg){
                    layer.alert(msg.msg, {icon: 6},function () {
                        // $(obj).parents("tr").remove();
                        layer.msg('已删除!',{icon:1,time:1000});
                    });
                },
                error : function(XMLHttpRequest, textStatus, errorThrown) {
                    alert(XMLHttpRequest.status + "," + textStatus);
                }
            });
        });
    }

    var sid = $("#sid").val();
    var layout = [
         {
            name: '层级',
            treeNodes: true,
            headerClass: 'value_col',
            colClass: 'value_col',
            style: 'width: 25%'
        },
        {
            name: '名称',
            headerClass: 'value_col',
            colClass: 'value_col',
            style: 'width: 10%',
            render: function (row) {
                return  row.title
            }
        },
        {
            name: '模块',
            headerClass: 'value_col',
            colClass: 'value_col',
            style: 'width: 10%',
            render: function (row) {
                return  row.module
            }
        },
        {
            name: '位置',
            headerClass: 'value_col',
            colClass: 'value_col',
            style: 'width: 10%',
            render: function (row) {
                return  row.position
            }
        },
        {
            name: 'url',
            headerClass: 'value_col',
            colClass: 'value_col',
            style: 'width: 10%',
            render: function (row) {
                return  row.url
            }

        },
        {
            name: '操作',
            headerClass: 'value_col',
            colClass: 'value_col',
            style: 'width: 20%',
            render: function (row) {
                if(row.name != "ROOT"){
                    return `<a  onclick="del(` + row.id+`,`+sid+ `)" href="javascript:;"><i class="layui-icon">&#xe640;</i></a>` +
                        `<a  href="{:url("Confermenu/details")}?id=`+row.id+`&sid=`+sid+`"><i class="layui-icon">&#xe642;</i> </a>`; //列渲染
                }else{
                    return " ";
                }
            }
        },
    ];

    layui.use(['form', 'tree', 'layer'], function() {
        var layer = layui.layer;

        var sid = $("#sid").val();
        // alert(sid);
        getmenu(sid);

    });

    function getmenu(sid){
        var page = 1;
        var limit = 10000;
        $.ajax({
            url:'list?sid='+sid,
            type:"post",
            dataType:"json",
            data : {page:page,limit:limit},
            success: function(data) {
               console.log(data);

                    layui.treeGird({
                        elem: '#demo', //传入元素选择器
                        nodes: test(data[0].intr),
                        layout: layout
                    });

            },
            error: function(XMLHttpRequest, textStatus, errorThrown) {
                alert(XMLHttpRequest.status);
                alert(XMLHttpRequest.readyState);
                alert(textStatus);
            },
        })
    }

    function test(data) {
        // var dat = null;
        console.log(data[0].children)
        var dat = [];
        for (var i = 0; i < data.length; i++) {
            if(data[i].parent == "ROOT"){
                if(data[i].children[0]){
                    dat[i] =
                        {
                            "id": i+data[i].uid,
                            "name": data[i].parent,
                            "title": data[i].translations[1].name,
                            "module": data[i].module,
                            "position": data[i].position,
                            "url": data[i].url,
                            "children": [{
                                "id": data[i].uid,
                                "name": data[i].translations[1].name,
                                "title": data[i].translations[1].name,
                                "module": data[i].module,
                                "position": data[i].position,
                                "url": data[i].url,
                                "children": test1(data[i].children)

                            },]

                        };
                }else{
                    dat[i] =
                        {
                            "id": i+data[i].uid,
                            "name": data[i].parent,
                            "title": data[i].translations[1].name,
                            "module": data[i].module,
                            "position": data[i].position,
                            "url": data[i].url,
                            "children": [{
                                "id": data[i].uid,
                                "name": data[i].translations[1].name,
                                "title": data[i].translations[1].name,
                                "module": data[i].module,
                                "position": data[i].position,
                                "url": data[i].url,

                            },]

                        };
                }

            }

        }
        return dat;
    }

    function test1(data) {
        console.log(data);
        if(data != undefined){
            var dat = [];
            for (var i = 0; i < data.length; i++) {
                dat[i] =
                    {
                        "id": data[i].uid,
                        "name": data[i].translations[1].name,
                        "title": data[i].translations[1].name,
                        "module": data[i].module,
                        "position": data[i].position,
                        "url": data[i].url,
                        "children": test1(data[i].children),
                    }
            }
            console.log(dat);
            return dat;
        }

    }
</script>
{include file="./layout/footer.html" /}